<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sealplayer
 *
 * @author Samuel
 */
class pvpbusiness_controller extends Controller {
	private $typearray;
	private $areaList;	//区号列表
	
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->typearray=array('1'=>translate('SEAL_PVP_PLAYER'),'2'=>translate('SEAL_BUSINESS_PLAYER'));
        $this->areaList = get_area_list_option();
    }

    function index() {
        $areahostArray = get_area_list_option();
		//print_r($_SESSION['hou']);
        $selectedArea = $_SESSION['area_selected_id'];
        //$this->view->assign("arealist", $this->form->select("area_selected_id", $areahostArray, $selectedArea, array('id' => 'area_selected_id')));
        $this->view->assign('arealist',$this->areaList);
        $this->view->display("seal/pvpbusiness/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $strResult = vpost($area_host . "action=3");
		
        $row = json_decode($strResult, true);
		$num=0;
        foreach ($row as $key=>$value)
        {
        	if (isset($value['pvp_end_time'])&&$value['pvp_end_time']!=0)
        	{
				if($value['pvp_end_time']<time())
				   continue;
        		$num+=1;				
				$value['type']=$this->typearray[$num];				
				$value['seal_end_time'] = get_datetime($value['pvp_end_time']);
				$rows[$num]=array('id' => $num.'_'.$value['role_id'], 'cell' => $value);
        	}
        	
        	if(isset($value['trade_end_time'])&&$value['trade_end_time']!=0)
        	{
				if($value['trade_end_time']<time())
				   continue;
        		$num+=1;			
				$value['type']=$this->typearray[$num];
				$value['seal_end_time'] = get_datetime($value['trade_end_time']);
				$rows[$num]=array('id' => $num.'_'.$value['role_id'], 'cell' => $value);
        	}
        }
        $total = count($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
		//$_SESSION['hou']=$rows;
        echo json_encode($result);
    }

    function add($id = 0) {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $str = array_to_stringparam($rowdata);
            if ($rowdata['type']==1)
            {
            	$seal_action=4;
            	$message=translate('SEAL_PVP_PLAYER');
            }
            else if ($rowdata['type']==2)
            {
            	$seal_action=3;
            	$message=translate('SEAL_BUSINESS_PLAYER');
            }
            $areaurl = get_area_list_url();
            $str = $areaurl[$rowdata['area']] . $str . "action=2&&seal_action=".$seal_action;
            $strResult = vpost($str);            
            
            $result=0;
            //操作失败或者连接失败
            if($strResult!='0' || $strResult===FALSE)
            {
                $result = 1;
            }
            else
            {
                insert_log(translate('MENU_SEAL_PVPBUSINESS'),translate('ADD'),$message,array("name"=>$rowdata['role_name']));
            }
            cp_tips($result == 0 ? "success" : "error");
        }
        $time = array( "86400" => translate('ONE_DAY'), "172800" => translate('TWO_DAYS'), "604800" => translate('ONE_WEEK'), "0" => translate('PERPETUAL'));
        $this->view->assign("timelist", $this->form->select("row[seal_time]", $time, '', array('id' => 'seal_time')));
        //$this->view->assign("arealist", $this->form->select("row[area_host]", get_area_list_host(), '', array('id' => 'area_host')));
        $this->view->assign('arealist',$this->areaList);
		$this->view->assign("type",$this->form->select("row[type]", $this->typearray, '', array('id' => 'type')));
        $this->view->display("seal/pvpbusiness/add");
    }

    function del($id = 0) {
        $host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : "";
        $type=explode('_', $id);
        //$_SESSION['hou']=$id;
        if ($type[0]==1)
        {
        	$seal_action=3;
        }
        else 
        {
        	$seal_action=4;
        }
        $strResult = vpost($host . "id=" . $type[1] . "&action=4&seal_action=".$seal_action);
		
        $res = (int) $strResult == 0 ? 1 : 0;
        if($res==1){
            insert_log(translate('ROLE_SEAL_DEL'),$id);
        }
        json_output($res, translate('C_TIPS_ERROR_TEXT'));
    }

}

?>
